基本屬性注入方法


  1. @Value

來源:

value.from.file=Value got from the file
listOfValues=A,B,C
valuesMap={key1: '1', key2: '2', key3: '3'}

列出幾個比較單純的方法

@Value("${value.from.file}")
private String valueFromFile;


@Value("${listOfValues}")
private String[] valuesArray; // [“A”, “B”, “C”]

**雖然此方式可以映射為List但我還是不偏好,因.split(',')包覆在""中因此不容易編譯時發現**
@Value("#{'${listOfValues}'.split(',')}")
private List<String> valuesList;

// Note that the values in the Map must be in single quotes.
@Value("#{${valuesMap}}")
private Map<String, Integer> valuesMap;

也可夠過 注入物件方式

@Component
public class PrimaryRepositoryConfiguration {

 @Primary
 @Bean(name = "primaryEntityManagerFactory")
 public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Autowired @Qualifier("primaryDataSource") DataSource dataSource) {
     LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
     em.setDataSource(dataSource);
     return em;
    }
}

這不是一個給新手的一個教學過程,也寫的不是很完整
希望大家多多包涵囉~
主要是給自己的一個紀錄,也分享給有需要的夥伴
這是一個心血來潮,產生的文章
若有喜歡或交流的部分都歡迎在下方留言,多多關照。

##attribute ##spring







你可能感興趣的文章

Command line:command line interface 介紹及基本指令

Command line:command line interface 介紹及基本指令

用函式來傳達你的心意> 0 <

用函式來傳達你的心意> 0 <

只是看看 JSON 檔案格式

只是看看 JSON 檔案格式






留言討論